package com.example.bausip;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class StudentInfoActivity extends Activity {

	ArrayList<NameEntity> courses;
	ArrayList<Item> courseItems;
	MyCustomAdapter dataAdapter;
	int student_id;
	TextView id_txt, name_txt,birth_txt,major_txt;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_student_info);
		Bundle extras = getIntent().getExtras();
		id_txt = (TextView) findViewById(R.id.uniId);
		name_txt = (TextView) findViewById(R.id.fullName);
		birth_txt = (TextView) findViewById(R.id.birth);
		major_txt = (TextView) findViewById(R.id.major);

		if(extras!=null)
			student_id = extras.getInt("student_id");
		getStudent();
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.student_info, menu);
		return true;
	}
	public void getcourses() {
		String serverURL = new myURL("read_courses.php?student_id="+student_id,true).getURL();
		RZHelper p = new RZHelper(serverURL, this, "setCourses", true);
		p.get();
	}
	public void getStudent() {
		String serverURL = new myURL("read_students.php?id="+student_id,true).getURL();
		RZHelper p = new RZHelper(serverURL, this, "setstudent", true);
		p.get();
	}
	public void setstudent(String s, String error) {
		Log.d("ray", "reply: " + s);
		Student current = new APIManager().getStudents(s).get(0);
		id_txt.setText(current.getId()+"");
		name_txt.setText(current.toString());
		birth_txt.setText(current.getBirthdate()+"");
		major_txt.setText(current.getMajorId()+"");
		getcourses();
	}

	public void setCourses(String s, String error) {
		Log.d("ray", "reply: " + s);
		NameEntity ne = null;
		courses = new ArrayList<NameEntity>();
		
		ArrayList<Course> myCs = new APIManager().getCourses(s);
		for(int i =0;i<myCs.size();i++)
		{
			Log.d("ray"," Course id" +myCs.get(i).getId() + " ,name: "+myCs.get(i).getCourse_name());
			ne = new NameEntity(myCs.get(i).getId(), myCs.get(i).getCourse_name(), "course");
			ne.setDescription(myCs.get(i).getCourse_description());
			courses.add(ne);
		}
		courseItems = new ArrayList<Item>();
		ListView listView = (ListView) findViewById(R.id.list);

		listView.setTextFilterEnabled(true);
		if (courses.size() == 0) {
			courseItems.add(new Item(0, "", getString(R.string.empty_list)));
		} else {
			for (int i = 0; i < courses.size(); i++) {
				Item it = new Item(courses.get(i).getId(), "",
						courses.get(i).toString());
				it.setCode(courses.get(i).getDescription());
				courseItems.add(it);
			}
			registerForContextMenu(listView);
			dataAdapter = new MyCustomAdapter(this, R.layout.name_list,
					courseItems);
			SharedMenu.adapter = dataAdapter;
			listView.setAdapter(dataAdapter);
			listView.setOnItemClickListener(new OnItemClickListener() {

				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
					if (dataAdapter.tmpList.size() > 0) {
						Intent i;
						i = new Intent(getBaseContext(),CourseActivity.class);
						i.putExtra("course_id",dataAdapter.tmpList.get(position).getId());
						startActivity(i);

					}
				}
			});
		}
	}

}
